package com.qiguliuxing.dts.wx.controller;

import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.common.util.ResponseUtils;
import com.qiguliuxing.dts.core.annotation.LoginUser;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.annotation.repeat.NoRepeatSubmit;
import com.qiguliuxing.dts.db.bean.SeckillGoodsVo;
import com.qiguliuxing.dts.service.seckill.WxSeckillService;
import com.qiguliuxing.dts.service.seckill.model.SeckillStatus;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.text.ParseException;
import java.util.List;

/**
 * 秒杀服务层
 *
 * @author 1197673878@qq.com
 * @since 2020年1月23日 下午9:32:49
 */
@RestController
@Validated
@RequestMapping("/wx/seckill")
@Api(tags = "秒杀相关Api")
public class WxSeckillController {
    private static final Logger logger = LoggerFactory.getLogger(WxSeckillController.class);

    @Autowired
    private WxSeckillService wxSeckillService;

    /**
     * 秒杀接口
     *
     * @param userId      用户Id
     * @param seckillGoodsId 秒杀商品id
     * @param productId   秒杀货品id
     * @param productId   秒杀货品数量
     *
     * @return 秒杀状态
     */
    @NoRepeatSubmit
    @GetMapping(path = "/doSeckill")
    @ApiOperation(value = "秒杀接口", response = String.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", paramType = "query", value = "用户Id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "seckillGoodsId", paramType = "query", value = "秒杀商品Id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "productId", paramType = "query", value = "货品Id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "number", paramType = "query", value = "秒杀数量", required = true, dataType = "Integer")})
    @WebLog(description = "秒杀接口")
    public Object doSeckill(@LoginUser Integer userId,
                          @RequestParam(value = "seckillGoodsId") int seckillGoodsId,
                          @RequestParam(value = "productId") int productId,
                          @RequestParam(value = "number") int number) {
        if (userId == null) {
            logger.error("用户未登录！！！");
            return ResponseUtils.unlogin();
        }
        SeckillStatus seckillStatus = wxSeckillService.doSeckill(userId, seckillGoodsId, productId, number);
        return ResponseUtils.ok(seckillStatus);
    }


    /**
     * 根据批次获取的列表数据,需要计算剩余量
     *
     * @param timeId 秒杀批次Id
     * @return 列表数据
     */
    @GetMapping("/batchGoods")
    @ApiOperation(value = "秒杀首页数据")
    @WebLog(description = "秒杀首页数据")
    public Object batchGoods(@LoginUser Integer userId, @RequestParam(value = "timeId", required = false) int timeId) {
        List<SeckillGoodsVo> seckillGoodsList = wxSeckillService.getBatchSeckillGoodByBatchId(userId, timeId);
        return ResponseUtils.ok(seckillGoodsList);
    }

    /**
     * 首页批次集合
     *
     * @return 首页批次集合
     * @throws ParseException
     */
    @GetMapping("/batchTimes")
    @ApiOperation(value = "首页批次集合信息")
    @WebLog(description = "首页批次集合信息", excludes = LogParamsConsts.RESPONSE)
    public Object batchTimes() {
        return ResponseUtils.ok(wxSeckillService.secondSeckillBatchInfo());
    }

    @GetMapping("subscribe")
    @ApiOperation(value = "秒杀消息订阅")
    @WebLog(description = "秒杀消息订阅")
    public Object subscribe(@LoginUser Integer userId,
                            @ApiParam(value = "秒杀商品ID") @NotNull Integer id,
                            @ApiParam(value = "时间状态（0今日, 2明日）") Integer status) {
        if (userId == null) {
            logger.error("秒杀消息订阅失败:用户未登录！！！");
            return ResponseUtils.unlogin();
        }
        wxSeckillService.subscribe(userId, id, status);
        return ResponseUtils.ok();
    }
}
